<?php
/**
 * 移动端基类通用控制器
 */
class ApiBaseController extends \Yaf\Controller_Abstract {

    public function init() {
        if($this->getRequest()->isOptions()) {
            $ua_config = \Yaf\Application::app()->getConfig()->app;
            header('Access-Control-Allow-Headers:Content-type,X-Auth-Token,X-Auth-UA,Cookies');
            header('Access-Control-Request-Method:POST');
            header('Access-Control-Allow-Origin:*');
            exit();
        }
        
/*        $ua = $this->getRequest()->getServer('HTTP_X_AUTH_UA');
        $ua_config = \Yaf\Application::app()->getConfig()->app;

        $m = $this->getRequest()->getModuleName();
        $c = $this->getRequest()->getControllerName();
        $a = $this->getRequest()->getActionName();
        $url = strtolower(\Vendor\Helper::url($c,$a));

        if($ua_config['ua'] != $ua && $m == "api" && !in_array($url,['/login/verify.do','/mobile/login/verify.do'])){
            return $this->errorJson(500,'系统繁忙001');
        }*/
    }

    //是否ajax请求
    public function isAjax() {
        if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") {
            return true;
        }
        return false;
    }

    /**
     * API接口成功返回数据结构
     */
    public function ajaxReturn($code = 0,$msg = "",$data = "") {
        header('Content-type:application/json;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        $res = ['code'=>$code,'msg'=>$msg,'data'=>$data];
        exit(json_encode($res));
    }

    //dataTable数据返回格式
    public function dataTable($data, $count){
        header('Content-type:application/json;charset=utf8');
        $res = array(
            'sEcho' => $_POST['draw'],
            'iTotalDisplayRecords' => $count,
            'iTotalRecords' => $count,
            'aaData' => $data,
        );
        exit(json_encode($res));
    }

    /**
     * @param null $name
     * @return mixed|null
     */
    public function get($name = null,$default = "",$isXss = true)
    {
        //静态路由没有$_GET
        if(!$_GET){
            $_GET = $this->getRequest()->getParams();
        }

        if(!$name){
            $data = $_GET;
        }elseif(isset($_GET[$name])){
            $data = $_GET[$name];
        }else{
            return $default;
        }

        if($isXss)
            return $this->xssClean($data);
        return $data;
    }

    /**
     * @param null $name
     * @return mixed|null
     */
    public function post($name = null,$default = "",$isXss = true)
    {
        if(!$name){
            $data = $_POST;
        }elseif(isset($_POST[$name])){
            $data = $_POST[$name];
        }else{
            return $default;
        }

        if($isXss)
            return $this->xssClean($data);
        return $data;
    }

    public function jsonData($isXss = true){
        $data = json_decode(file_get_contents('php://input'), true);
        if($isXss)
            return $this->xssClean($data);
        return $data;
    }

    /**
     * @param $data
     * @return mixed
     */
    public function xssClean($data)
    {
        if(is_array($data)){
            return filter_var_array($data, FILTER_SANITIZE_STRING);
        }else{
            return filter_var($data, FILTER_SANITIZE_STRING);
        }
    }

    /**
     * 设置布局
     * @param string $layout 布局文件
     */
    public function setLayout($layout)
    {
        $this->getResPonse()->layout = function($content) use ($layout){
            //$content子视图的内容
            return $this->getView()->render($layout, ['content'=>$content]);
        };
    }
}
